package view;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Graphics2D;
import java.awt.Point;
import java.io.IOException;

import model.Slot;
import model.State;

public class GraphicsUtils {
	/**
	 * Posizione della scritta Turno giocatore:
	 */
	private static final Point textPlayerTurnPosition = new Point(25, 8);
	private static final Point textNewMatchPosition = new Point(25, 32);
	private static final Point coinPlayerTurnPosition = new Point(175, -14);
	private static final Point textPlayer1TypePosition = new Point(350, 5);
	private static final Point textPlayer2TypePosition = new Point(350, 30);

	public static int slotSize = 60;
	public static final int cellSize = 80;
	public static final int gameBoardOffset = 20;
	public static final int slotOffset = 30;
	public static final int frameHeaderOffset = 22;
	public static final int headerHeight = 50;
	public static final int footerHeight = 40;

	public static int getFrameHeight(int rows) {
		return rows * cellSize + 2 * gameBoardOffset + frameHeaderOffset
				+ footerHeight + headerHeight;
	}

	public static int getFrameWidth(int cols) {
		return cols * cellSize + 2 * gameBoardOffset;
	}

	public static int getBoardHeight(int rows) {
		return rows * cellSize + 2 * gameBoardOffset + footerHeight
				+ headerHeight;
	}

	public static int getBoardWidth(int cols) {
		return cols * cellSize + 2 * gameBoardOffset;
	}

	public static void paintPlayerTurn(int offset, Graphics2D g2d, int turn) {
		if (turn == 1)
			g2d.setColor(Color.RED);
		else
			g2d.setColor(Color.YELLOW);

		g2d.fillOval(coinPlayerTurnPosition.x, coinPlayerTurnPosition.y
				+ offset, 30, 30);

	}

	public static void paintEndGame(Graphics2D g2d, State state, int cols) {

		try {
			Font f = Font.createFont(
					Font.TRUETYPE_FONT,
					GraphicsUtils.class.getClassLoader().getResourceAsStream(
							"fonts/forza4font.ttf"));
			g2d.setFont(f.deriveFont(80f));
		} catch (FontFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		String text = "";
		switch (state) {
		case DRAW:
			text = "DRAW!";
			g2d.setColor(Color.WHITE);
			break;
		case PLAYING:
			break;
		case VICTORY_1:
			text = "PLAYER 1 WINS!";
			g2d.setColor(Color.RED);
			break;
		case VICTORY_2:
			text = "PLAYER 2 WINS!";
			g2d.setColor(Color.YELLOW);
			break;
		default:
			break;

		}

		double stringLen = g2d.getFontMetrics().getStringBounds(text, g2d)
				.getWidth();
		double start = ((double) getBoardWidth(cols)) / 2 - stringLen / 2;
		g2d.drawString(text, (int) start, headerHeight + 10);

	}

	public static void paintHoveredColumn(Graphics2D g2d, int turn, int col) {
		if (turn == 1)
			g2d.setColor(Color.RED);
		else
			g2d.setColor(Color.YELLOW);

		g2d.fillOval(slotOffset + col * cellSize, 5, slotSize, slotSize);

	}

	public static void paintGameBoard(Graphics2D g2D, int rows, int cols,
			Slot[][] slots, int lastPlayedCol, int lastPlayedRow) {
		g2D.setColor(Color.BLUE);
		g2D.fillRoundRect(gameBoardOffset, gameBoardOffset + headerHeight,
				cellSize * cols, rows * cellSize, 60, 60);

		for (int i = 0; i < rows; i++)
			for (int j = 0; j < cols; j++) {
				switch (slots[i][j]) {
				case FREE:
					g2D.setColor(Color.WHITE);
					break;
				case PLAYER1:
					g2D.setColor(Color.RED);
					break;
				case PLAYER2:
					g2D.setColor(Color.YELLOW);
					break;
				default:
					break;

				}

				g2D.fillOval(slotOffset + j * cellSize, slotOffset + i
						* cellSize + headerHeight, slotSize, slotSize);
			}

		if (lastPlayedCol != -1) {
			g2D.setColor(Color.GREEN);
			g2D.setStroke(new BasicStroke(3));
			g2D.drawOval(slotOffset + lastPlayedCol * cellSize, slotOffset
					+ lastPlayedRow * cellSize + headerHeight, slotSize,
					slotSize);
		}
	}

	public static void paintPlayersInfo(int offset, Graphics2D g2d,
			String type1, String type2, int cols) {
		g2d.setColor(Color.WHITE);
		Font f = null;
		try {
			f = Font.createFont(
					Font.TRUETYPE_FONT,
					GraphicsUtils.class.getClassLoader().getResourceAsStream(
							"fonts/forza4font.ttf"));
			g2d.setFont(f.deriveFont(18f));
		} catch (FontFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// g2d.setFont(FontUtils.getLevelFont());
		String text1 = "Player 1: " + type1;
		String text2 = "Player 2: " + type2;
		double stringLen1 = g2d.getFontMetrics().getStringBounds(text1, g2d)
				.getWidth();
		double stringLen2 = g2d.getFontMetrics().getStringBounds(text2, g2d)
				.getWidth();
		double stringLen = stringLen1 > stringLen2 ? stringLen1 : stringLen2;
		double start = getBoardWidth(cols) - stringLen - gameBoardOffset;

		g2d.drawString(text1, (int) start, textPlayer1TypePosition.y + offset);
		g2d.drawString(text2, (int) start, textPlayer2TypePosition.y + offset);

		// Draw Players TYPE
		g2d.setColor(Color.RED);
		g2d.fillOval((int) start - 20, textPlayer1TypePosition.y + offset - 14,
				15, 15);
		g2d.setColor(Color.YELLOW);
		g2d.fillOval((int) start - 20, textPlayer2TypePosition.y + offset - 14,
				15, 15);


		g2d.setColor(Color.WHITE);
		if(f!=null)
			g2d.setFont(f.deriveFont(30f));

		g2d.drawString("Player turn:", textPlayerTurnPosition.x,
				textPlayerTurnPosition.y + offset);
		
		if(f!=null)
			g2d.setFont(f.deriveFont(12f));
		g2d.setColor(Color.GREEN);
		g2d.drawString("Press 'N' to start a new match", textNewMatchPosition.x,
				textNewMatchPosition.y + offset);
	}
}
